package owen.com.dxc;

import android.content.ContentValues;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.KeyEvent;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import java.util.Timer;
import java.util.TimerTask;

import owen.com.dxc.db.DBHelper;

public class Register extends AppCompatActivity {

    private EditText account,r_name,r_password,r_password2,age,job,remark;
    private Button registerBtn;
    private TextView tv;
    private DBHelper dbHelper;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.register);
        init();
        dbHelper = new DBHelper(this);
    }

    protected void init(){
        account = (EditText)findViewById(R.id.account);
        r_name = (EditText)findViewById(R.id.r_name);
        r_password = (EditText)findViewById(R.id.r_password);
        r_password2 = (EditText)findViewById(R.id.r_password2);
        age = (EditText)findViewById(R.id.age);
        job = (EditText)findViewById(R.id.job);
        remark = (EditText)findViewById(R.id.remark);

        registerBtn = (Button)findViewById(R.id.registerBtn);
        registerBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String msg = checkRegister();
                if("success".equals(msg)){
                    save();
                    Toast.makeText(Register.this, "注册成功", Toast.LENGTH_SHORT).show();

                    Timer timer = new Timer();
                    timer.schedule(new TimerTask() {
                        @Override
                        public void run() {
                            startActivity(new Intent(Register.this, Login.class));
                        }
                    }, 1000);
                }else{
                    Toast.makeText(Register.this, msg, Toast.LENGTH_LONG).show();
                }
            }
        });

        tv = (TextView)findViewById(R.id.toLoginTv);
        tv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(Register.this, Login.class));
            }
        });
    }

    public String checkRegister(){
        String name = r_name.getText().toString().trim();
        if(name.length() == 0){
            return "你是sei？你是不是那爱玩屎的阿拉蕾";
        }else{
            SQLiteDatabase db = dbHelper.getWritableDatabase();
            Cursor cursor = db.rawQuery("select * from user where name = ?", new String[]{name});
            if(cursor.getCount() > 0){
                cursor.close();
                return "用户名已被注册";
            }
            cursor.close();
        }

        if(r_password.getText().toString().trim().length() < 8){
            return "密码至少8位数";
        }

        if(!r_password2.getText().toString().equals(r_password.getText().toString())){
            return "两次密码不一致";
        }

        return "success";
    }

    public void save(){
        SQLiteDatabase db = dbHelper.getWritableDatabase();
        ContentValues values = new ContentValues();
        values.put("account", account.getText().toString());
        values.put("name", r_name.getText().toString());
        values.put("password", r_password.getText().toString());

        String ageStr = age.getText().toString().trim();
        if("".equals(ageStr)){
            ageStr = null;
            values.put("age", ageStr);
        }else{
            values.put("age", Integer.valueOf(age.getText().toString()));
        }

        values.put("job", job.getText().toString());
        values.put("remark", remark.getText().toString());

        db.insert("user", null, values);
        db.close();
    }

}
